//
//  ProcessBar.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/15.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class ProcessBar: UIView {

    var step: Int = 1
    
    let dots: [UIView] = {
        var imgViews: [UIView] = []
        for i in 0...stepNameStrings.count - 1 {
            let imgView = UIView()
            imgViews.append(imgView)
        }
        return imgViews
    }()
    
    let lines: [UIView] = {
        var imgViews: [UIView] = []
        for i in 0...stepNameStrings.count - 2 {
            let imgView = UIView()
            imgViews.append(imgView)
        }
        return imgViews
    }()
    
    let stepNames: [UILabel] = {
        var labels: [UILabel] = []
        for i in 0...stepNameStrings.count - 1 {
            let label = UILabel()
            label.font = AppFont(size: 12)
            label.textColor = UIColor.gray
            label.text = stepNameStrings[i]
            labels.append(label)
        }
        return labels
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        for view in dots {
            self.addSubview(view)
        }
        for view in lines {
            self.addSubview(view)
        }
        for label in stepNames {
            self.addSubview(label)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let dotWidth: CGFloat = (self.bounds.width - 24) / 18
        let lineWidth: CGFloat = (self.bounds.width - 20 - dotWidth * CGFloat(stepNameStrings.count)) / CGFloat(stepNameStrings.count - 1)
        
        for i in 0...dots.count - 1 {
            dots[i].frame = CGRect(x: 12 + CGFloat(i) * (dotWidth + lineWidth), y: 0, width: dotWidth, height: dotWidth)
            dots[i].layer.cornerRadius = dotWidth / 2
            dots[i].layer.masksToBounds = true
            dots[i].layer.borderWidth = dotWidth / 4
            dots[i].backgroundColor = UIColor.white
            if i < step {
                dots[i].layer.borderColor = proceededColor.cgColor
            } else {
                dots[i].layer.borderColor = notProceededColor.cgColor
            }
        }
        
        for i in 0...lines.count - 1 {
            lines[i].frame = CGRect(x: 12 + dotWidth + CGFloat(i) * (dotWidth + lineWidth), y: dotWidth / 2 + 0.5, width: lineWidth, height: 1)
            if (i + 1) < step {
                lines[i].backgroundColor = proceededColor
            } else {
                lines[i].backgroundColor = notProceededColor
            }
        }
        
        let textMaxSize = CGSize(width: bounds.width, height: 14)
        for i in 0...stepNames.count - 1 {
            let width = TextSize.textSize(text: stepNames[i].text!, font: AppFont(size: 12), maxSize: textMaxSize).width
            stepNames[i].frame = CGRect(x: CGFloat(i) * (dotWidth + lineWidth), y: dotWidth + 5, width: width, height: 14)
        }
    }
}
